﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RWSBeeldbank.Common;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace RWSBeeldbank.DataModel
{
    /// <summary>
    /// Base class for <see cref="MediaObjectDataItem"/> and <see cref="MediaObjectDataGroup"/> that 
    /// defines properties common to both.
    /// </summary>
    [Windows.Foundation.Metadata.WebHostHidden]
    public abstract class MediaObjectDataCommon : RWSBeeldbank.Common.BindableBase
    {
        internal static Uri _baseUri = new Uri("ms-appx:///");

        public MediaObjectDataCommon(String uniqueId, String title, String _largeImagePath)
        {
            this._uniqueId = uniqueId;
            this._title = title;
            this._largeImagePath = _largeImagePath;
        }

        private string _uniqueId = string.Empty;
        public string UniqueId
        {
            get { return this._uniqueId; }
            set { this.SetProperty(ref this._uniqueId, value); }
        }

        private string _title = string.Empty;
        public string Title
        {
            get { return this._title; }
            set { this.SetProperty(ref this._title, value); }
        }

        private ImageSource _largeImage = null;
        private String _largeImagePath = null;

        public Uri LargeImagePath
        {
            get
            {
                return new Uri(MediaObjectDataCommon._baseUri, this._largeImagePath);
            }
        }

        public ImageSource LargeImage
        {
            get
            {
                if (this._largeImage == null && this._largeImagePath != null)
                {
                    this._largeImage = new BitmapImage(new Uri(MediaObjectDataCommon._baseUri, this._largeImagePath));
                }
                return this._largeImage;
            }

            set
            {
                this._largeImagePath = null;
                this.SetProperty(ref this._largeImage, value);
            }
        }

        public void SetLargeImage(String path)
        {
            // path = http://beeldbank.rws.nl/MediaService/313367
            //
            // must result in
            //
            // _largeImagePath = http://beeldbank.rws.nl/MediaService/[313367] to get the full source image
            //
            this._largeImage = null;

            this._largeImagePath = path.Replace(Constants.HTTP, Constants.HTTPS).Replace(Constants.TILE_WIDTH_SOURCE, Constants.LARGE_WIDTH_TARGET) + Constants.TILE_WIDTH_TARGET2;

            this.OnPropertyChanged("LargeImage");
        }

        public void SetLargeImageFromId(String id)
        {
            this._largeImage = null;
            this._largeImagePath = "https://beeldbank.rws.nl/Home/DisplayDetail/" + id;
            this.OnPropertyChanged("LargeImage");
        }

        public string GetLargeImageUri()
        {
            return _largeImagePath;
        }
    }
}
